package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import db.DBConnection;
import db.DBProject;
import db.DBProjectSubContractor;
import db.DBProjectUsedItem;
import db.IFProject;
import db.IFProjectSubContractor;
import db.IFProjectUsedItem;
import model.Customer;
import model.Item;
import model.Project;
import model.ProjectSubContractor;
import model.ProjectUsedItem;
import model.SubContractor;

public class ProjectController 
{
	private static ProjectController instance = null;

	public static ProjectController getInstance()
	{
		if(instance == null)
			instance = new ProjectController();

		return instance;
	}

	public ProjectController()
	{

	}

	public int addNewProject(String name, int drawing_no, int prod_quantity, String dimensions, String description, String deadline, Customer customer, double price, String projLink, String comments, ArrayList<SubContractor> subs, ArrayList<Double> subPrice) throws SQLException
	{
		int result = -2;

		try
		{
			Project project = new Project();

			project.setName(name);
			project.setDrawingNo(drawing_no);
			project.setProdQuantity(prod_quantity);
			project.setDescription(description);
			project.setDeadline(deadline);
			project.setPrice(price);
			project.setCustomer(customer);
			project.setProjLink(projLink);
			project.setComments(comments);
			project.setDimensions(dimensions);

			DBConnection.startTransaction();

			IFProject dbProject = new DBProject();
			result = dbProject.createProject(project,subs, subPrice);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateProject(int projectID, String name, String dimensions, int drawing_no, int prod_quantity, String description, String deadline, Customer customer, double price, String projLink, String comments) throws SQLException
	{
		int result = -2;

		try
		{
			Project project = new Project();

			project.setName(name);
			project.setServiceID(projectID);
			project.setDimensions(dimensions);
			project.setDrawingNo(drawing_no);
			project.setProdQuantity(prod_quantity);
			project.setDescription(description);
			project.setDeadline(deadline);
			project.setPrice(price);
			project.setCustomer(customer);
			project.setProjLink(projLink);
			project.setComments(comments);

			DBConnection.startTransaction();

			IFProject dbProject = new DBProject();
			result = dbProject.updateProject(project);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int transferProject(int offerID, String name, int drawing_no, int prod_quantity,String dimensions, String description, String deadline, Customer customer, double price, String projLink, String comments, ArrayList<SubContractor> subs, ArrayList<Double> subPrice) throws SQLException
	{
		int result = -2;

		try
		{
			System.out.println("TEST IS : ");
			Project project = new Project();
			project.setName(name);
			project.setDrawingNo(drawing_no);
			project.setProdQuantity(prod_quantity);
			project.setDimensions(dimensions);
			project.setDescription(description);
			project.setDeadline(deadline);
			project.setPrice(price);
			project.setCustomer(customer);
			project.setProjLink(projLink);
			project.setComments(comments);

			DBConnection.startTransaction();

			IFProject dbProject = new DBProject();
			result = dbProject.transferProject(offerID,project, subs, subPrice);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
		return result;
	}

	public int updateProjectPrice(int projectID) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.updateProjectPrice(projectID);
	}

	public int activateProject(int projectID) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.activateProject(projectID);
	}

	public Project findProjectByName(String name) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.findProjectByName(name);
	}
	public Project findProjectByID(int id) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.findProjectByID(id);
	}

	public ArrayList<Project> searchProjectByName(String name, boolean active) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.searchProjectByName(name, active);
	}

	public ArrayList<Project> searchProjectByCity(String city, boolean active) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.searchProjectByCity(city, active);
	}

	public ArrayList<Project> searchProjectByCountry(String country, boolean active) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.searchProjectByCustomer(country, active);
	}

	public ArrayList<Project> searchProjectByCustomer(String customer, boolean active) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.searchProjectByCustomer(customer, active);
	}

	public ArrayList<Project> searchProjectByEverything(String search, boolean active) throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.searchProjectByEverything(search, active);
	}

	public ArrayList<Project> getAllActiveProjects() throws SQLException
	{
		IFProject dbProject = new DBProject();

		return dbProject.getAllActiveProjects();
	}

	public ArrayList<ProjectUsedItem> getProjectItems(Project project) throws SQLException
	{
		IFProjectUsedItem dbProjectUsedItem = new DBProjectUsedItem();

		return dbProjectUsedItem.getAllProjectUsedItem(project);
	}

	public int addProductUsedItem(Project project, Item item, int quantity) throws SQLException
	{		
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFProjectUsedItem dbProjectUsedItem = new DBProjectUsedItem();
			result = dbProjectUsedItem.addProjectUsedItem(project, item, quantity);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int deleteProjectUsedItem(Project project, int itemID) throws SQLException
	{		
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFProjectUsedItem dbProjectUsedItem = new DBProjectUsedItem();
			result = dbProjectUsedItem.deleteProjectUsedItem(project, itemID);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateProjectUsedItem(Project project, int itemID, int quantity) throws SQLException
	{
		int result = -1;

		try
		{
			DBConnection.startTransaction();

			IFProjectUsedItem dbProjectUsedItem = new DBProjectUsedItem();
			result = dbProjectUsedItem.updateProjectUsedItem(project, itemID, quantity);

			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}
	public int addProjectSubContractor(Project project, SubContractor sub, double price) throws SQLException
	{
		int result = -1;
		
		try
			{
				DBConnection.startTransaction();
				
				IFProjectSubContractor ps = new DBProjectSubContractor();
				result = ps.addProjectSubContractor(project, sub, price);
				
				DBConnection.commitTransaction();
			}
		catch(Exception e)
			{
				DBConnection.rollbackTransaction();
			}
		return result;
	}
	public int deleteProjectSubContractor(Project project, int subID) throws SQLException
	{
		int result = -1;
		
		try
			{
				DBConnection.startTransaction();
				
				IFProjectSubContractor ps = new DBProjectSubContractor();
				result = ps.deleteProjectSubContractor(project, subID);
				
				DBConnection.commitTransaction();
			}
		catch(Exception e)
			{
				DBConnection.rollbackTransaction();
			}
		return result;
	}
	public int updateProjectSubContractor(Project project, int subID, double price) throws SQLException
	{
		int result = -1;
		
		try
			{
				DBConnection.startTransaction();
				
				IFProjectSubContractor ps = new DBProjectSubContractor();
				result = ps.updateProjectSubContractor(project, subID, price);
				
				DBConnection.commitTransaction();
			}
		catch(Exception e)
			{
				DBConnection.rollbackTransaction();
			}
		return result;
	}

	public ArrayList<ProjectSubContractor> getAllProjectSubContractor(Project project) throws SQLException
	{
		IFProjectSubContractor ps = new DBProjectSubContractor();
		return ps.getAllProjectSubContractor(project);
	}

	public double getTotalProjectSubContractorPrice(int projectID) throws SQLException
	{
		IFProjectSubContractor ps = new DBProjectSubContractor();
		return ps.getTotalProjectSubContractorPrice(projectID);
	}
}